@layout($syslayout)

@section('content')
<div class="page-header">
     <h3>{{ Str::title($flow) }} > {{ HTML::link('admin/modul/step/'.URI::segment(5), 'Step') }}</h3>
</div>
<div class="row-fluid">
  <a href="#addRuleModal" role="button" class="btn pull-right" data-toggle="modal" style='margin-bottom:10px'><i class="icon-plus"></i>&nbsp;New Rule</a>
</div>
<div class="row-fluid">
  <div class="span12 pull-left">
    <div id="rulelist" class="row-fluid">
      {{ $rulelist }}
    </div>
  </div>
</div>

<!-- MODAL -->

<div id="addRuleModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Add New Rule</h3>
  </div>
  <div class="modal-body">
      {{ Form::open('admin/user/role', 'POST', array('id' => 'addRuleForm', 'class' => 'form-horizontal')) }}
      {{ Form::hidden('sf_id',URI::segment(5)) }}
      {{ Form::hidden('sfr_id') }}
      {{ Form::control_group(Form::label('sfs_id', 'Step'),Form::select('sfs_id', $steps)); }}
      {{ Form::control_group(Form::label('sfr_next', 'Next'),Form::select('sfr_next', $steps)); }}
      {{ Form::control_group(Form::label('sfr_state', 'State'),Form::mini_text('sfr_state',null,array('placeholder'=>'State','required')));}}
      {{ Form::control_group(Form::label('sur_id', 'Role'),Form::select('sur_id', $allrole)); }}
      {{ Form::control_group(Form::label('sfr_page', 'Page'),Form::select('sfr_page', $pagelist)); }}
      {{ Form::close()}}
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button id="addRuleBtn" class="btn btn-primary">Save</button>
  </div>
</div>



@endsection

@section('scripts')
<script>

  function deleteRule(id){

    $.post('{{ url('admin/modul/deleterule'); }}', "id="+id+"&flowid="+{{ URI::segment(5) }} ,function(data) {
          sourcedata = data;
        }).success(function() {
            sourcedata = jQuery.parseJSON(sourcedata);
            $( "#rulelist" ).empty().append( sourcedata );
            datasource();
            window.location.replace('{{ url("admin/modul/rule/".URI::segment(5))}}');
          });
  }


  $('#addRuleBtn').click(function() {

      $.post('{{ url('admin/modul/rule/'.URI::segment(5)); }}', $("#addRuleForm").serialize(),function(data) {
              sourcedata = data;
            }).success(function() { 
              sourcedata = jQuery.parseJSON(sourcedata);
              $("#rulelist" ).empty().html( sourcedata );
              $("#addRuleForm [name='sfs_id']").val('');
              $("#addRuleForm [name='sfr_next']").val('');
              $("#addRuleForm :input[name='sfr_state']").val('');
              $("#addRuleForm [name='sur_id']").val('');
              $("#addRuleForm [name='sfr_page']").val('');
              $('#addRuleModal').modal('hide');
              window.location.replace('{{ url("admin/modul/rule/".URI::segment(5))}}');
            });
    

  });


  
  function editRule(id){

      $('#addRuleModal').modal('toggle');
      $('#addRuleModal #myModalLabel').empty().html('Edit Step')

      $.get('{{ url('admin/modul/ruleinfo'); }}', { stepid: id},function(data,status){

        for (x in data)
        {   
          $('#addRuleForm [name="'+ x +'"]' ).val(data[x]);
        }

      },"json");

  }


  function datasource(){

      $.get('{{ url('admin/modul/resetnavdata'); }}',  function(data,status){

      for (x in data)
      {   

        var datavalue = data[x];
        var $el = $("[name=" + x + "]");
        $el.empty();

        $.each(datavalue, function(key, value) {
          $el.append($("<option></option>").attr("value", key).text(value));

        });

      }

      },"json");

  }

</script>
@endsection